package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberMapper;
import com.itheima.health.dao.OrderMapper;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@DubboService
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private MemberMapper memberMapper;

    /**
     * @Author: 位帅帅
     * @Date: 12:30 2021/1/15
     * @Params: []
     * @RerurnType: java.util.Map<java.lang.String,java.lang.Object>
     * @Description: 获取运营数据
    */
    @Override
    public Map<String, Object> getBusinessReportData() {
        Map<String,Object> map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(new Date());
        //设置报表生成日期
        map.put("reportDate",today);
        Order queryTodayVisitsNumber = new Order();
        queryTodayVisitsNumber.setOrderDate(new Date());
        //设置今日预约人数
        int todayOrderNumber = orderMapper.selectCount(queryTodayVisitsNumber);
        map.put("todayOrderNumber",todayOrderNumber);
        //设置今日到诊人数
        queryTodayVisitsNumber.setOrderStatus(Order.ORDERSTATUS_YES);
        int todayVisitsNumber = orderMapper.selectCount(queryTodayVisitsNumber);
        map.put("todayVisitsNumber",todayVisitsNumber);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        cal.set(Calendar.MILLISECOND,0);
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        Calendar weekFirstDay = (Calendar) cal.clone();
        Calendar weekLastDay = (Calendar) cal.clone();
        Example queryThisWeekVisitsNumber = new Example(Order.class);
        Example.Criteria criteria = queryThisWeekVisitsNumber.createCriteria();
        criteria.andGreaterThanOrEqualTo("orderDate",weekFirstDay.getTime());
        criteria.andLessThanOrEqualTo("orderDate",weekLastDay.getTime());
        //设置本周预约人数
        int thisWeekOrderNumber = orderMapper.selectCountByExample(queryThisWeekVisitsNumber);
        map.put("thisWeekOrderNumber",thisWeekOrderNumber);
        //设置本周到诊人数
        criteria.andEqualTo("orderStatus",Order.ORDERSTATUS_YES);
        int thisWeekVisitsNumber = orderMapper.selectCountByExample(queryThisWeekVisitsNumber);
        map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        Calendar monthFirstDay = (Calendar) cal.clone();
        monthFirstDay.set(Calendar.DAY_OF_MONTH,1);
        Calendar monthLastDay = (Calendar) cal.clone();
        int maxDayOfMonth = monthLastDay.getActualMaximum(Calendar.DAY_OF_MONTH);
        monthLastDay.set(Calendar.DAY_OF_MONTH,maxDayOfMonth);
        Example queryThisMonthVisitsNumber = new Example(Order.class);
        Example.Criteria criteria2 = queryThisMonthVisitsNumber.createCriteria();
        criteria2.andGreaterThanOrEqualTo("orderDate",monthFirstDay.getTime());
        criteria2.andLessThanOrEqualTo("orderDate",monthLastDay.getTime());
        //设置本月预约人数
        int thisMonthOrderNumber = orderMapper.selectCountByExample(queryThisMonthVisitsNumber);
        map.put("thisMonthOrderNumber",thisMonthOrderNumber);
        //设置本月到诊人数
        criteria2.andEqualTo("orderStatus",Order.ORDERSTATUS_YES);
        int thisMonthVisitsNumber = orderMapper.selectCountByExample(queryThisMonthVisitsNumber);
        map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        //设置今日新增会员数量
        Member queryTodayNewMember = new Member();
        queryTodayNewMember.setRegTime(new Date());
        int todayNewMember = memberMapper.selectCount(queryTodayNewMember);
        map.put("todayNewMember",todayNewMember);
        //设置本周新增会员数量
        Example queryThisWeekNewMember = new Example(Member.class);
        Example.Criteria criteria3 = queryThisWeekNewMember.createCriteria();
        criteria3.andGreaterThanOrEqualTo("regTime",weekFirstDay.getTime());
        criteria3.andLessThanOrEqualTo("regTime",weekLastDay.getTime());
        int thisWeekNewMember = memberMapper.selectCountByExample(queryThisWeekNewMember);
        map.put("thisWeekNewMember",thisWeekNewMember);
        //设置本月新增
        Example queryThisMonthNewMember = new Example(Member.class);
        Example.Criteria criteria4 = queryThisMonthNewMember.createCriteria();
        criteria4.andGreaterThanOrEqualTo("regTime",monthFirstDay.getTime());
        criteria4.andLessThanOrEqualTo("regTime",monthLastDay.getTime());
        int thisMonthNewMember = memberMapper.selectCountByExample(queryThisMonthNewMember);
        map.put("thisMonthNewMember",thisMonthNewMember);
        //设置会员总数
        int totalMember = memberMapper.selectCount(null);
        map.put("totalMember",totalMember);
        //设置热门套餐
        List<Map<String,Object>> hotSetmeal = orderMapper.findHostSetmeal(4);
        map.put("hotSetmeal",hotSetmeal);
        log.info("[运营数据-查询]map: {}",map);
        return map;
        /*"data":{
               "reportDate":"2019-04-25",//日期
               "todayVisitsNumber":0,//今日到诊人数
               "thisWeekVisitsNumber":0,//本周到诊人数
               "thisMonthVisitsNumber":0,//本月到诊人数
               "todayNewMember":0,//今日新增会员
               "thisMonthNewMember":2,//本月新增会员
               "thisWeekNewMember":0,//本周新增会员
               "totalMember":10,//总会员
               "thisMonthOrderNumber":2,//本月预约人数
               "todayOrderNumber":0,//今日预约人数
               "thisWeekOrderNumber":0,//本周预约人数
               "hotSetmeal":[//热门套餐
                        {"proportion":0.4545,"name":"粉红珍爱(女)升级TM12项筛查体检套餐","setmeal_count":5},
                        {"proportion":0.1818,"name":"阳光爸妈升级肿瘤12项筛查体检套餐","setmeal_count":2},
                        {"proportion":0.1818,"name":"珍爱高端升级肿瘤12项筛查","setmeal_count":2},
                        {"proportion":0.0909,"name":"孕前检查套餐","setmeal_count":1}
               ],
        }*/
    }
}
